/*
 * @(#)SetupOptionDocumentRelationshipDAObject.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.setup.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.setup.bean.SetupOption;

/**
 *  SetupOptionDocumentRelationshipDAObject main DAO
 *
 *  @author           Kennedy Lam
 *  @company          DCIVision Limited
 *  @creation date    28/07/2003
 *  @version          $Revision: 1.3.32.1 $
 */
public class SetupOptionDocumentRelationshipDAObject extends AbstractSetupOptionDAObject {

  public static final String REVISION = "$Revision: 1.3.32.1 $";


  public static final String TABLE_NAME  = "SETUP_OPTION_DOC_RELATIONSHIP";
  public static final String OPTION_NAME = "RELATIONSHIP_NAME";
  public static final String OPTION_CODE = "RELATIONSHIP_CODE";
  public static final boolean HAS_OPTION_CODE = true;
  public static final String DEFAULT_ORDER = OPTION_NAME;
  //public static final boolean IS_SOFT_DELETE = false;
  /**
   *
   */
  public SetupOptionDocumentRelationshipDAObject() {
    super();
  }

  /**
   * @param dbConn
   */
  public SetupOptionDocumentRelationshipDAObject(Connection dbConn) {
    super(dbConn);
  }

  /**
   * @param sessionContainer
   * @param dbConn
   */
  public SetupOptionDocumentRelationshipDAObject(
    SessionContainer sessionContainer,
    Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  /* (non-Javadoc)
   * @see com.dcivision.framework.dao.AbstractDAObject#initDBSetting()
   */
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.sOptionNameCol = OPTION_NAME;
    this.sOptionCodeCol = OPTION_CODE;
    this.sDefaultOrder = DEFAULT_ORDER;
    this.bHasOptionCode = HAS_OPTION_CODE;
    //this.isSoftDeleteEnabled = IS_SOFT_DELETE;
    super.initDBSetting();
  }

  /**
   * deleteObject method delete the setupOption which is not used.
   * @param setupOption
   * @return isNotUsed
   */
  public synchronized boolean deleteObjectByNotUsed(SetupOption setupOption) throws ApplicationException{
    boolean isNotUsed=true;
    StringBuffer sql = new StringBuffer();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    synchronized (dbConn) {
      try {
        String optionCode = setupOption.getOptionCode();
        sql.append(" SELECT * FROM MTM_DOCUMENT_RELATIONSHIP WHERE RELATIONSHIP_TYPE =? ");
        pstmt = dbConn.prepareStatement(sql.toString());
        this.setPrepareStatement(pstmt, 1, optionCode);
        rs = pstmt.executeQuery();
        if (rs.next()) {
          isNotUsed=false;
}
      }catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(pstmt);
      }
    }
    super.deleteObject(setupOption);
    return isNotUsed;
  }
}
